package com.hejia.bulbul.controller;

import java.text.ParseException;
import java.util.HashMap;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.Parameter;
import com.hejia.alauda.entity.example.ParameterExample;
import com.hejia.alauda.service.CustomService;
import com.hejia.alauda.utils.Pager;
import com.hejia.bulbul.entity.DataTablesReturnObject;
import com.hejia.bulbul.entity.JSONParam;

/**
 * 
 * @author jatin
 *
 */
@Controller
@RequestMapping("custom")
public class CustomController extends BaseController {

	@Resource
	private CustomService customService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String customList(Pager<Parameter> pager, ModelMap model) {

		Pager<Parameter> customs = customService.findCustom(pager);
		model.put("customs", customs);
		return "/custom/custom_list";

	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String customAdd(ModelMap model) {
		return "/custom/custom_input";
	}

	/**
	 * 添加完保存
	 * 
	 * @throws ParseException
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String saveCustom(Parameter parameter, ModelMap model,
			RedirectAttributes redirectAttributes) throws ParseException {
		if (customService.saveCustom(parameter)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "自定义参数添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "自定义参数添加失败,可能是参数已存在");
		}

		return "redirect:/custom/list";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateCustom(@PathVariable Long id, ModelMap model) {
		Parameter custom = customService.getCustomById(id);
		model.put("custom", custom);

		return "/custom/custom_input";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	public String saveUpdateCustom(Parameter parameter,
			RedirectAttributes redirectAttributes) {
		if (customService.modifyCustom(parameter)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "自定义参数修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "自定义参数修改失败,可能是参数已存在");
		}

		return "redirect:/custom/list";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String deleteCustom(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (customService.deleteCustom(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "自定义参数删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "自定义参数删除失败");
		}

		return "redirect:/custom/list";
	}

	/**
	 * 以ajax格式返回列表请求
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "customAsAjax", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesReturnObject<Parameter> customAsAjax(
			@RequestBody JSONParam[] params) {
		HashMap<String, String> paramMap = JSONParam.convertToMap(params);

		// 分页信息
		Pager<Parameter> pager = new Pager<Parameter>();
		int start = Integer.parseInt(paramMap.get("iDisplayStart"));
		int length = Integer.parseInt(paramMap.get("iDisplayLength"));
		pager.setPageSize(length);
		pager.setPageNumber(start / length + 1);

		// 查询条件
		ParameterExample exp = new ParameterExample();

		String sSearch = paramMap.get("sSearch");
		if (StringUtils.isNotEmpty(sSearch)) {
			exp.appendCriterion("code like  ", "%" + sSearch + "%");
		}
		// 排序
		int iSortingCols = Integer.parseInt(paramMap.get("iSortingCols"));
		for (int i = 0; i < iSortingCols; i++) {
			String iSortCol = paramMap.get("iSortCol_" + i);
			String mDataProp = paramMap.get("mDataProp_" + iSortCol);
			String sSortDir = paramMap.get("sSortDir_" + i);
			if (i > 0) {
				exp.setOrderByClause(exp.getOrderByClause() + ",");
				exp.setOrderByClause(exp.getOrderByClause() + mDataProp + " "
						+ sSortDir);
			} else {
				exp.setOrderByClause(mDataProp + " " + sSortDir);
			}
		}

		pager = customService.findCustom(pager, exp);

		DataTablesReturnObject<Parameter> dtr = new DataTablesReturnObject<Parameter>(
				pager.getTotalCount(), paramMap.get("sEcho"), pager.getList());
		return dtr;
	}

}
